package com.xxx;

import java.util.Random;
import java.util.Scanner;

public class Clearance {

    Scanner s = new Scanner(System.in);
    Random r1 = new Random();//行
    Random r2 = new Random();//列
    Mine[][] mine = new Mine[10][10];
    boolean b = true;
    int death; //1表示存活 0表示死亡

    public void game(){
        death = 1;
        while (true) {
            System.out.println("=====扫雷小游戏=====");
            System.out.println("1.开始游戏");
            System.out.println("2.退出游戏");
            System.out.println("请输入选择的序号：");

            String num = s.next();
            switch (num){
                case "1":
                    start();
                    break;
                case "2":
                    return;
                default:
                    System.out.println("输入错误，请重新输入~~");
            }
        }

    }

    public void start(){
        System.out.println("游戏操作：");
        System.out.println("\t1.输入行号及列号来选中要翻开的“?”进行操作，可以选择插旗（#）或者直接翻开。");
        System.out.println("\t2.如果翻开“*”则表示地雷，则游戏结束，如果翻开的是数字，则表示该格周围的雷数。");
        System.out.println("\t3.标记出全部地雷，并且没有“?”，则闯关成功，游戏结束.");
        System.out.println("**********************************************************************");

        randomMine();//初始化雷区
        while (b) {
            show();//展示雷区
            mineClearance();//开始扫雷
            all();//判断是否翻完或者被炸死
        }
        show();
        if (death == 1){
            System.out.println("恭喜你排雷成功！");
        }
        else {
            System.out.println("很遗憾！你被炸死了。");
        }

    }
//初始化雷区
    public void randomMine(){
        int line;//行
        int column;//列
        //初始化
        for (int i = 0; i < mine.length; i++) {
            for (int j = 0; j < mine[i].length; j++) {
                mine[i][j] = new Mine();
                mine[i][j].setOne("0");
                mine[i][j].setTwo("0");
                mine[i][j].setThree(0);
            }
        }

        //放雷并得出周围雷数
        for (int i = 0; i < mine.length; i++) {
            while (true) {
                column = r1.nextInt(10);
                line = r2.nextInt(10);
                if (mine[line][column].getOne().equals("1")!=true){
                    mine[line][column].setOne("1");
                    for (int j = line-1; j <= line+1; j++) {
                        if(j<0)
                            continue;
                        if (j>mine.length-1)
                            continue;
                        for (int k = column-1; k <=column+1 ; k++) {
                            if(k<0)
                                continue;
                            if (k>mine.length-1)
                                continue;
                            int num = mine[j][k].getThree();
                            mine[j][k].setThree(++num);
                        }

                    }
                    break;
                }
            }

        }
    }
//展示棋盘
    public void show(){
        int a = 8;
        System.out.print("\t");
        for (int i = 0; i < mine.length; i++) {
            System.out.print(i+" ");

        }
        System.out.println();
        System.out.print("   ");
        for (int i = 0; i < mine.length*2; i++) {
            System.out.print("*");
        }
        System.out.println();
        //无雷区

        while (a>0) {
            for (int i = 0; i < mine.length; i++) {
                for (int j = 0; j < mine[i].length; j++) {
                    if(mine[i][j].getThree() == 0 && mine[i][j].getTwo().equals("0") != true){
                        for (int k = i-1; k <=i+1 ; k++) {
                            if(k<0){
                                continue;
                            }

                            if (k>mine.length-1)
                            {
                                continue;
                            }

                            for (int l = j - 1; l <= j + 1; l++) {
                                if (l<0){
                                    continue;
                                }

                                if(l> mine.length-1)
                                {
                                    continue;
                                }

                                mine[k][l].setTwo("3");

                            }
                        }
                    }
                }
            }
            a--;
        }
        for (int i = 0; i < mine.length; i++) {
            System.out.print(i+" * ");
            for (int j = 0; j < mine[i].length; j++) {
                if (mine[i][j].getTwo().equals("0")){
                    System.out.print("? ");
                }
                else if(mine[i][j].getTwo().equals("1")){
                    System.out.print("# ");
                }
                else if(mine[i][j].getTwo().equals("2")){
                    System.out.print("* ");

                }
                else if(mine[i][j].getTwo().equals("3")){

                    System.out.print(mine[i][j].getThree()+ " ");
                }


            }
            System.out.print("\n");
        }
        System.out.print("   ");
        for (int i = 0; i < mine.length*2; i++) {
            System.out.print("*");
        }
        System.out.println();

    }

    //判断是否全部翻完
    public void all(){
        for (int i = 0; i < mine.length; i++) {
            for (int j = 0; j < mine[i].length; j++) {
                if(mine[i][j].getOne().equals("1")!=true ){
                    if(mine[i][j].getTwo().equals("0") != true){
                        b = false;
                    }
                }
                else if (mine[i][j].getTwo().equals("2")){
                    b= false;
                    death = 0;
                    return;
                }
                else {
                   b = true;
                   return;
                }

            }
        }

    }

    //开始扫雷
    public void mineClearance(){
        int line,column;
        String a ;
        while (true) {
            System.out.println("请输入行号：");
            line = s.nextInt();
            System.out.println("请输入列号：");
            column = s.nextInt();
            System.out.println("====请选择操作====");
            System.out.println("1.标记");
            System.out.println("2.翻开");
            a = s.next();
            switch (a){
                case "1":
                    sign(line,column);
                    return;
                case "2":

                    if (mine[line][column].getTwo().equals("3")){
                        System.out.println("这个已经翻过了");
                    }
                    else {
                        open(line,column);
                    }
                    return;
                default:
                    System.out.println("不存在该操作，请重新输入~~");
            }
        }


    }

    //标记

    public void sign(int line, int column){
        mine[line][column].setTwo("1");
    }

    //翻开
    public void open(int line, int column){
        if (mine[line][column].getOne().equals("1")){
            mine[line][column].setTwo("2");
        }
        else {
            mine[line][column].setTwo("3");
        }


    }
}
